##
## Author: Hank Anderson <hank@statease.com>
## Description: Ported from portion of OpenBLAS/Makefile.system
##              Sets C related variables.
include(CheckCCompilerFlag)

if (${CMAKE_C_COMPILER_ID} MATCHES "IntelLLVM")
  set(CCOMMON_OPT "${CCOMMON_OPT} -fp-model=consistent")
  set(GCC_VERSION 100)
endif ()

if (${CMAKE_C_COMPILER_ID} STREQUAL "GNU" OR ${CMAKE_C_COMPILER_ID} STREQUAL "LSB" OR ${CMAKE_C_COMPILER_ID} MATCHES "Clang")
  set(CCOMMON_OPT "${CCOMMON_OPT} -Wall")
  set(COMMON_PROF "${COMMON_PROF} -fno-inline")
  set(NO_UNINITIALIZED_WARN "-Wno-uninitialized")
  set(GCC_VERSION ${CMAKE_C_COMPILER_VERSION})

  if (QUIET_MAKE)
    set(CCOMMON_OPT "${CCOMMON_OPT} ${NO_UNINITIALIZED_WARN} -Wno-unused")
  endif ()

  if (NO_BINARY_MODE)

    if (MIPS32)
        set(CCOMMON_OPT "${CCOMMON_OPT} -mabi=32")
      set(BINARY_DEFINED 1)
    endif ()

    if (MIPS64)
      if (BINARY64)
        set(CCOMMON_OPT "${CCOMMON_OPT} -mabi=64")
      else ()
        set(CCOMMON_OPT "${CCOMMON_OPT} -mabi=n32")
      endif ()
      set(BINARY_DEFINED 1)
    endif ()

    if (${CORE} STREQUAL "LOONGSON3A" OR ${CORE} STREQUAL "LOONGSON3B")
      set(CCOMMON_OPT "${CCOMMON_OPT} -march=mips64")
      set(FCOMMON_OPT "${FCOMMON_OPT} -march=mips64")
    endif ()

    if (LOONGARCH64)
      if (BINARY64)
	CHECK_C_COMPILER_FLAG("-mabi=lp64d" COMPILER_SUPPORT_LP64D_ABI)
        if(COMPILER_SUPPORT_LP64D_ABI)
	  set(CCOMMON_OPT "${CCOMMON_OPT} -mabi=lp64d")
	else()
	  set(CCOMMON_OPT "${CCOMMON_OPT} -mabi=lp64")
	endif ()
      else ()
	CHECK_C_COMPILER_FLAG("-mabi=ilp32d" COMPILER_SUPPORT_ILP32D_ABI)
	if(COMPILER_SUPPORT_ILP32D_ABI)
	  set(CCOMMON_OPT "${CCOMMON_OPT} -mabi=ilp32d")
	else()
	  set(CCOMMON_OPT "${CCOMMON_OPT} -mabi=lp32")
	endif ()
      endif ()
      set(BINARY_DEFINED 1)
    endif ()

    if (CMAKE_SYSTEM_NAME STREQUAL "AIX")
      set(BINARY_DEFINED 1)
    endif ()
  endif ()

  if (NOT BINARY_DEFINED)
    if (BINARY64)
      set(CCOMMON_OPT "${CCOMMON_OPT} -m64")
    else ()
      set(CCOMMON_OPT "${CCOMMON_OPT} -m32")
    endif ()
  endif ()
endif ()

if (${CMAKE_C_COMPILER_ID} STREQUAL "PGI")
  if (BINARY64)
    set(CCOMMON_OPT "${CCOMMON_OPT} -tp p7-64")
  else ()
    set(CCOMMON_OPT "${CCOMMON_OPT} -tp p7")
  endif ()
endif ()

if (${CMAKE_C_COMPILER_ID} STREQUAL "NVHPC")
  if (POWER)
    set(CCOMMON_OPT "${CCOMMON_OPT} -tp pwr8")
  else ()
    set(CCOMMON_OPT "${CCOMMON_OPT} -tp px")
  endif ()
endif ()

if (${CMAKE_C_COMPILER_ID} STREQUAL "PATHSCALE")
  if (BINARY64)
    set(CCOMMON_OPT "${CCOMMON_OPT} -m64")
  else ()
    set(CCOMMON_OPT "${CCOMMON_OPT} -m32")
  endif ()
endif ()

if (${CMAKE_C_COMPILER_ID} STREQUAL "OPEN64")

  if (MIPS64)

    if (NOT BINARY64)
      set(CCOMMON_OPT "${CCOMMON_OPT} -n32")
    else ()
      set(CCOMMON_OPT "${CCOMMON_OPT} -n64")
    endif ()

    if (${CORE} STREQUAL "LOONGSON3A")
      set(CCOMMON_OPT "${CCOMMON_OPT} -loongson3 -static")
    endif ()

    if (${CORE} STREQUAL "LOONGSON3B")
      set(CCOMMON_OPT "${CCOMMON_OPT} -loongson3 -static")
    endif ()

  else ()

    if (BINARY64)
      set(CCOMMON_OPT "${CCOMMON_OPT} -m32")
    else ()
      set(CCOMMON_OPT "${CCOMMON_OPT} -m64")
    endif ()
  endif ()
endif ()

if (${CMAKE_C_COMPILER_ID} STREQUAL "SUN")
  set(CCOMMON_OPT "${CCOMMON_OPT} -w")
  if (X86)
    set(CCOMMON_OPT "${CCOMMON_OPT} -m32")
  else ()
    set(CCOMMON_OPT "${CCOMMON_OPT} -m64")
  endif ()
endif ()

if (${CORE} STREQUAL SKYLAKEX)
  if (NOT DYNAMIC_ARCH)
    if (NOT NO_AVX512)
      set (CCOMMON_OPT "${CCOMMON_OPT} -march=skylake-avx512")
    endif ()
  endif ()
endif ()

if (${CORE} STREQUAL COOPERLAKE)
  if (NOT DYNAMIC_ARCH)
    if (NOT NO_AVX512)
      if (${GCC_VERSION} VERSION_GREATER 10.1 OR ${GCC_VERSION} VERSION_EQUAL 10.1)
        set (CCOMMON_OPT  "${CCOMMON_OPT} -march=cooperlake")
      else ()
        set (CCOMMON_OPT "${CCOMMON_OPT} -march=skylake-avx512")
      endif()  
    endif ()
  endif ()
endif ()

if (${CORE} STREQUAL SAPPHIRERAPIDS)
  if (NOT DYNAMIC_ARCH)
    if (NOT NO_AVX512)
      if (${GCC_VERSION} VERSION_GREATER 11.0 OR ${GCC_VERSION} VERSION_EQUAL 11.0)
        set (CCOMMON_OPT  "${CCOMMON_OPT} -march=sapphirerapids")
      else ()
        set (CCOMMON_OPT "${CCOMMON_OPT} -march=skylake-avx512")
      endif()  
    endif ()
  endif ()
endif ()

if (${CORE} STREQUAL ZEN)
  if (HAVE_AVX512VL)
    if (NOT DYNAMIC_ARCH)
      if (NOT NO_AVX512)
        if (${GCC_VERSION} VERSION_GREATER 13.0 OR ${GCC_VERSION} VERSION_EQUAL 13.0)
          set (CCOMMON_OPT  "${CCOMMON_OPT} -march=znver4")
        else ()
          set (CCOMMON_OPT "${CCOMMON_OPT} -march=skylake-avx512")
        endif ()
      endif ()
    endif ()
  endif ()
endif ()

if (${CORE} STREQUAL A64FX)
  if (NOT DYNAMIC_ARCH)
    if (${GCC_VERSION} VERSION_GREATER 11.0 OR ${GCC_VERSION} VERSION_EQUAL 11.0)
      set (CCOMMON_OPT  "${CCOMMON_OPT} -march=armv8.2-a+sve -mtune=a64fx")
    else ()
      set (CCOMMON_OPT "${CCOMMON_OPT} -march=armv8.2-a+sve")
    endif()
  endif ()
endif ()

if (${CORE} STREQUAL NEOVERSEN2)
  if (NOT DYNAMIC_ARCH)
    if (${CMAKE_C_COMPILER_ID} STREQUAL "PGI" AND NOT NO_SVE)
	set (CCOMMON_OPT  "${CCOMMON_OPT} -Msve_intrinsics -march=armv8.5-a+sve+sve2+bf16 -mtune=neoverse-n2")
    else ()
      if (${GCC_VERSION} VERSION_GREATER 10.4 OR ${GCC_VERSION} VERSION_EQUAL 10.4)
	set (CCOMMON_OPT  "${CCOMMON_OPT} -march=armv8.5-a+sve+sve2+bf16 -mtune=neoverse-n2")
      else ()
	set (CCOMMON_OPT "${CCOMMON_OPT} -march=armv8.2-a+sve")
      endif()
    endif ()
  endif ()
endif ()

if (${CORE} STREQUAL NEOVERSEV1)
  if (NOT DYNAMIC_ARCH)
    if (${CMAKE_C_COMPILER_ID} STREQUAL "PGI" AND NOT NO_SVE)
	set (CCOMMON_OPT  "${CCOMMON_OPT} -Msve_intrinsics -march=armv8.4-a+sve -mtune=neoverse-v1")
    else ()
      if (${GCC_VERSION} VERSION_GREATER 10.4 OR ${GCC_VERSION} VERSION_EQUAL 10.4)
        set (CCOMMON_OPT  "${CCOMMON_OPT} -march=armv8.4-a+sve -mtune=neoverse-v1")
      else ()
        set (CCOMMON_OPT "${CCOMMON_OPT} -march=armv8.2-a+sve")
      endif()
    endif()
  endif ()
endif ()

if (${CORE} STREQUAL NEOVERSEN1)
  if (NOT DYNAMIC_ARCH)
    if (${GCC_VERSION} VERSION_GREATER 9.4 OR ${GCC_VERSION} VERSION_EQUAL 9.4)
      set (CCOMMON_OPT  "${CCOMMON_OPT} -march=armv8.2-a+sve -mtune=neoverse-n1")
    else ()
      set (CCOMMON_OPT "${CCOMMON_OPT} -march=armv8.2-a+sve")
    endif()
  endif ()
endif ()

if (${CORE} STREQUAL ARMV8SVE)
  if (NOT DYNAMIC_ARCH)
    if (${CMAKE_C_COMPILER_ID} STREQUAL "PGI" AND NOT NO_SVE)
	set (CCOMMON_OPT  "${CCOMMON_OPT} -Msve_intrinsics -march=armv8-a+sve")
    else ()
      set (CCOMMON_OPT "${CCOMMON_OPT} -march=armv8-a+sve")
    endif ()
  endif ()
endif ()

if (${CORE} STREQUAL CORTEXA510)
  if (NOT DYNAMIC_ARCH)
    set (CCOMMON_OPT "${CCOMMON_OPT} -march=armv8-a+sve")
  endif ()
endif ()

if (${CORE} STREQUAL CORTEXA710)
  if (NOT DYNAMIC_ARCH)
    set (CCOMMON_OPT "${CCOMMON_OPT} -march=armv8-a+sve")
  endif ()
endif ()

if (${CORE} STREQUAL CORTEXX1)
  if (NOT DYNAMIC_ARCH)
    set (CCOMMON_OPT "${CCOMMON_OPT} -march=armv8.2-a")
  endif ()
endif ()

if (${CORE} STREQUAL CORTEXX2)
  if (NOT DYNAMIC_ARCH)
    set (CCOMMON_OPT "${CCOMMON_OPT} -march=armv8-a+sve")
  endif ()
endif ()

if (${CORE} STREQUAL POWER10)
  if (NOT DYNAMIC_ARCH)
    if (${GCC_VERSION} VERSION_GREATER 10.2 OR ${GCC_VERSION} VERSION_EQUAL 10.2)
      set (CCOMMON_OPT  "${CCOMMON_OPT} -mcpu=power10 -mtune=power10 -mvsx -fno-fast-math")
    else ()
      message(FATAL_ERROR "Compiler GCC ${GCC_VERSION} does not support Power10." )
    endif()
  endif ()
endif ()

if (${CORE} STREQUAL POWER9)
  if (NOT DYNAMIC_ARCH)
    if (${GCC_VERSION} VERSION_GREATER 5.0 OR ${GCC_VERSION} VERSION_EQUAL 5.0)
      set (CCOMMON_OPT  "${CCOMMON_OPT} -mcpu=power9 -mtune=power9 -mvsx -fno-fast-math")
    else ()
      set (CCOMMON_OPT  "${CCOMMON_OPT} -mcpu=power8 -mtune=power8 -mvsx -fno-fast-math")
      message(WARNING "Compiler GCC ${GCC_VERSION} does not fully support Power9.")
    endif ()
  endif ()
endif ()

if (${CORE} STREQUAL POWER8)
  if (NOT DYNAMIC_ARCH)
    set (CCOMMON_OPT  "${CCOMMON_OPT} -mcpu=power8 -mtune=power8 -mvsx -fno-fast-math")
  endif ()
endif ()

# With -mcpu=970 added it compiles, but library is broken, at least on macOS. If someone
# tests on *BSD or Linux and adds this flag, please make sure it is not used for macOS case.
if (${CORE} STREQUAL PPC970)
  if (NOT DYNAMIC_ARCH)
    set (CCOMMON_OPT  "${CCOMMON_OPT} -mtune=970 -maltivec -fno-fast-math")
  endif ()
  if (APPLE)
    set (CCOMMON_OPT  "${CCOMMON_OPT} -force_cpusubtype_ALL")
  endif ()
endif ()

# -mcpu=G4 seems to work fine, but perhaps avoid it for the sake of consistency?
if (${CORE} STREQUAL PPCG4)
  if (NOT DYNAMIC_ARCH)
    set (CCOMMON_OPT  "${CCOMMON_OPT} -mtune=G4 -maltivec -fno-fast-math")
  endif ()
  if (APPLE)
    set (CCOMMON_OPT  "${CCOMMON_OPT} -force_cpusubtype_ALL")
  endif ()
endif ()

if (NOT DYNAMIC_ARCH)
	if (HAVE_AVX2)
        set (CCOMMON_OPT  "${CCOMMON_OPT} -mavx2")
	endif ()
	if (HAVE_AVX)
        set (CCOMMON_OPT  "${CCOMMON_OPT} -mavx")
	endif ()
	#	if (HAVE_FMA3)
	#set (CCOMMON_OPT  "${CCOMMON_OPT} -mfma")
	#endif ()
	if (HAVE_SSE)
	set (CCOMMON_OPT  "${CCOMMON_OPT} -msse")
	endif ()
	if (HAVE_SSE2)
        set (CCOMMON_OPT  "${CCOMMON_OPT} -msse2")
	endif ()
	if (HAVE_SSE3)
        set (CCOMMON_OPT  "${CCOMMON_OPT} -msse3")
	endif ()
	if (HAVE_SSSE3)
        set (CCOMMON_OPT  "${CCOMMON_OPT} -mssse3")
	endif ()
	if (HAVE_SSE4_1)
	set (CCOMMON_OPT  "${CCOMMON_OPT} -msse4.1")
	endif ()
endif()
